package com.nit.bike.controller.bike;

import com.alibaba.fastjson.JSONObject;
import com.nit.bike.dto.bike.RideDTO;
import com.nit.bike.dto.system.TokenDTO;
import com.nit.bike.dto.utils.OpResultDTO;
import com.nit.bike.service.bike.CardService;
import com.nit.bike.service.bike.RideService;
import com.nit.bike.utils.AuthorizationUtil;
import com.nit.bike.utils.HttpRequestReader;
import com.nit.bike.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/ride")
public class RideController {

    @Resource
    private RideService rideService;
    private final Logger logger = LoggerFactory.getLogger(RideController.class);

    /**
     * 查询租借卡信息
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public OpResultDTO query(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            JSONObject json = HttpRequestReader.getJsonObject(request);
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            Long userId;
            if (tokenDTO.getRoleId() == 1 && json != null) {
                userId = json.getLong("userId");
                userId = userId == null ? tokenDTO.getAccountId() : userId;
            } else {
                userId = tokenDTO.getAccountId();
            }
            op.setMessage("success");
            op.setResult(rideService.query(userId));
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 申请租借卡
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token, @RequestBody RideDTO rideDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO.getRoleId() != 1 || rideDTO.getUserId() == null) {
                rideDTO.setUserId(tokenDTO.getAccountId());
            }
            if (tokenDTO.getRoleId() == 1 || rideDTO.getUserId().equals(tokenDTO.getAccountId())) {
                op = rideService.add(rideDTO);
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 更新租借卡信息
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestHeader(name = "Access-Token") String token, @RequestBody RideDTO rideDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                op.setResult(rideService.update(rideDTO));
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 挂失租借卡
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            JSONObject json = HttpRequestReader.getJsonObject(request);
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            Long userId;
            if (tokenDTO.getRoleId() == 1) {
                userId = json.getLong("userId");
                userId = userId == null ? tokenDTO.getAccountId() : userId;
            } else {
                userId = tokenDTO.getAccountId();
            }
            Long cardId = json.getLong("cardId");
            op.setResult(rideService.remove(cardId.toString()));
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }
}
